﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;

namespace Hired
{
    /*  Answer to a Question    */
    // !NOTE -- userID must be added
    // !NOTE -- this is because it does not have FK
    public static class DBAnswer
    {
        public static void AddAnswer(Answer answer)
        {
            answer.AnswerID = "tempvalue";
            using (hireddbEntities db = new hireddbEntities())
            {
                db.AddToAnswers(answer);
                db.SaveChanges();
            }
        }

        public static Answer RetrieveAnswer(string answerID)
        {
            Answer answer = new Answer();
            using (hireddbEntities db = new hireddbEntities())
            {
                    answer =
                    (from a in db.Answers
                    where a.AnswerID == answerID
                    select a).SingleOrDefault();
            }
            return answer;
        }

        public static bool UpdateAnswer(Answer ans)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Answer answerToUpdate = RetrieveAnswer(ans.AnswerID);
                    db.Attach(answerToUpdate);
                    answerToUpdate.Contents = ans.Contents;
                    answerToUpdate.Timestamp = answerToUpdate.Timestamp;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool DeleteAnswer(Answer ans)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Answer answerToDelete = RetrieveAnswer(ans.AnswerID);
                    db.Attach(answerToDelete);
                    db.DeleteObject(answerToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteAnswerById(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Answer answerToDelete = RetrieveAnswer(id);
                    db.Attach(answerToDelete);
                    db.DeleteObject(answerToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static List<Answer> searchAnswer(string searchQuery)
        {
            searchQuery = searchQuery.ToUpper();
            using (hireddbEntities db = new hireddbEntities())
            {
               IEnumerable<Answer> searchResults =
                    from a in db.Answers
                    where 
                    a.Contents.ToUpper().Contains(searchQuery)
                    select a;
                return searchResults.ToList<Answer>();
            }
        }
    }
}